// pages/member/course/CourseHistory/CourseHistory.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    histroyInfo: [],
    // more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    isShowNoHistory: '', // 是否显示提示
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._getData();
  },
  _getData: function (success) {
    var that = this;
    // 初次获取数据
    app._getNetWork({
      url: app.getNetAddresss('plugin.video-demand.api.video-demand-member.get-scan-history'),
      success: function (res) {
        console.log(res.data, '1111121132');
        console.log(res.data.data.length == 0, '对');

        if (res.data.data.length == 0) {
          that.setData({
            isShowNoHistory: true
          });
        } else {
          that.setData({
            isShowNoHistory: false
          });
        }
        console.log(that.data.isShowNoHistory, '判断');

        if (res.data.result == 1 && !app._isTextEmpty(res.data.data)) {
          let _data = res.data.data;

          that.setData({
            histroyInfo: _data.data,
            total_page: _data.last_page
          });
          success && success(res);
        } else {
          console.error(res);
        }
      },
      fail: function (res) {
        console.error(res);
      }
    });
  },
  clearCourse: function () {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确认全部清除吗？',
      success(res) {
        if (res.confirm) {
          app._getNetWork({
            url: app.getNetAddresss('plugin.video-demand.api.video-demand-member.historical-purge'),
            success: function (res) {
              if (res.data.result == 1) {
                that.setData({
                  histroyInfo: []
                });
                wx.showToast({
                  icon: 'none',
                  title: '清除成功',
                  duration: 1500
                });
              } else {
                console.error(res.data);
              }
            },
            fail: function (res) {
              console.error(res);
            }
          });
        } else if (res.cancel) {
          console.error(res);
        }
      }
    });
  },
  toCourse: function (e) {
    let itemid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2?id=' + itemid
      // url: '/packageA/detail_v2/detail_v2?id=' + itemid
    });
  },
  _getMoreData() {
    var that = this;
    let urlStr = app.getNetAddresss('plugin.video-demand.api.video-demand-member.get-scan-history');
    if (this.data.page >= this.data.total_page) {
      that.setData({
        isLoadMore: false
      });
      return;
    } else {
      that.setData({
        page: this.data.page + 1
      });
      urlStr += '&page=' + that.data.page;
      app._getNetWork({
        url: urlStr,
        success: function (res) {
          if (res.data.result == 1) {
            let _data = res.data.data;
            that.setData({
              isLoadMore: true,
              histroyInfo: that.data.histroyInfo.concat(_data.data)
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false
            });
            return;
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.setNavigationBarTitle({
      title: '观看历史'
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      isLoadMore: true,
      page: 1,
      total_page: 0
    });
    this._getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // 加载更多
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
});
